new Vue({
    el: '#app',
    data() {
        return {
            form: {
                Function_Approval_SerialNumber:'',
                Approval_Info_SerialNumber: '',
                Enterprise_Info_SerialNumber: $.cookie('enterprise'),
            },
            rules: {
                Approval_Info_SerialNumber:[{required: true, message: '请选择审批模板', trigger: 'change'}],
            },
            approval_info: [],
            loading: false
        }
    },
    mounted() {
        const Function_Approval_SerialNumber = sessionStorage.getItem("Function_Approval_SerialNumber")
        this.form.Function_Approval_SerialNumber = Function_Approval_SerialNumber
        this.getData(Function_Approval_SerialNumber);
    },
    methods: {
        getData(Primary_Key) {
            const _this = this;
            $.ajax({
                url: DomainName + '/getInquiryIDOriginal?token=' + $.cookie('token'),
                type: 'POST',
                data: {
                    Data: 'function_approval_detail',
                    Primary_Key,
                    Primary_Key_Name: 'Function_Approval_SerialNumber'
                },
                success: function (res) {
                    if (res.length > 0) {
                        _this.form = res[0];
                    }
                },
                error: function () {
                    _this.$message.error("操作失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                    _this.loading = false
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            })
            $.ajax({
                url: DomainName + '/getApprovalIDAction?token=' + $.cookie('token'),
                type: 'POST',
                data: {
                    Function_Approval_SerialNumber: Primary_Key,
                    Enterprise_Info_SerialNumber: $.cookie('enterprise')
                },
                success: function (res) {
                    console.log(res);
                    if (res.Status == 'success') {
                        _this.approval_info = res.Data
                    }
                },
                error: function () {
                    _this.$message.error("操作失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                    _this.loading = false
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            })
        },
        submitForm(formName) {
            let _this = this
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    _this.loading = true
                    const { add, modify } = _this.deal()
                    let data = {}
                    if (add == null) {
                        data = { 
                            Data: JSON.stringify({ Modify: { function_approval_detail: [modify] } }),
                            Operation_Name:'启用功能模块'
                        }
                    } else if(modify == null){
                        data = { 
                            Data: JSON.stringify({Add: { function_approval_detail: [add] }}) ,
                            Operation_Name:'启用功能模块'
                        }
                    }
                    $.ajax({
                        url: DomainName + '/getEditAction?token=' + $.cookie("token"),
                        type: 'POST',
                        data,
                        success: function (res) {
                            _this.$message.success("提交成功")
                            window.history.go(-1);
                        },
                        error: function () {
                            _this.$message.error("操作失败，请稍后再试")
                        },
                        complete: function (XMLHttpRequest) {
                            _this.loading = false
                            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                                window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                            }
                        }
                    })
                } else {
                    return false;
                }
            });
        },
        deal() {
            if (!Object.keys(this.form).some(item => item.includes('_Original'))) {
                const add = deepClone(this.form)
                return { add , modify:null };
            } else {
                this.form.Row_Status = '0';
                const modify = deepClone(this.form)
                return { add:null , modify };
            }
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    }
})